import React, {useContext} from 'react';
import {StyleSheet, View, Alert, Button} from 'react-native';
import StorageManager from '../Storage/StorageManager';
import {LoginContext} from '../Stacks/HomeStack';
import {useNavigation} from '@react-navigation/native';

const Category = () => {
  const loginState = useContext(LoginContext);
  const navigation: any = useNavigation();

  const goToLoginClick = () => {
    navigation.navigate('Login');
  };

  const removePress = () => {
    StorageManager.removeItem('Login')
      .then(() => {
        loginState.setLogin(false);
        Alert.alert('Logout success!');
      })
      .catch(() => {
        Alert.alert('Oops! Remove item error.');
      });
  };

  const clearPress = () => {
    StorageManager.clearAll();
  };

  return (
    <View style={styles.containerView}>
      {loginState.login ? (
        <Button title="Logout" onPress={removePress} />
      ) : (
        <Button title="Login" onPress={goToLoginClick} />
      )}
      <Button title="Clear all" onPress={clearPress} />
    </View>
  );
};

const styles = StyleSheet.create({
  containerView: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
});

export default Category;
